package com.teemor.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teemor.auth.bean.po.User;
import com.teemor.auth.bean.vo.req.CreateUserReq;
import com.teemor.auth.service.UserService;
import com.teemor.common.bean.utils.enums.DataStatus;
import com.teemor.common.bean.utils.tmpl.BaseController;
import com.teemor.common.bean.utils.tmpl.ResponseBen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author zhoulk
 * @Description:
 * @date: 2020/5/29
 */
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/add")
    public ResponseBen createUser(@RequestBody CreateUserReq req) {
        ResponseBen responseBen = new ResponseBen<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (req.getAccountType() == 0) {
            queryWrapper.eq("phone", req.getAccount());
        } else {
            queryWrapper.eq("email", req.getAccount());
        }
        List<User> users = userService.list(queryWrapper);
        if (CollectionUtils.isEmpty(users)) {
            User user = new User();
            user.setCreateTime(new Date());
            if (req.getAccountType() == 0) {
                user.setPhone(req.getAccount());
            } else {
                user.setEmail(req.getAccount());
            }
            user.setPwd(DigestUtils.md5Hex(req.getPwd()));
            user.setUserName(req.getUserName());
            user.setDataState(DataStatus.USE.value());
            userService.save(user);
        } else {
            return returnFailedObject("该帐号已被使用");
        }
        return responseBen;
    }



}
